﻿using AbpIntegrationDemo.Api.Applications.Orders.GraphTypes;
using AbpIntegrationDemo.Api.Domains.Orders;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories;

namespace AbpIntegrationDemo.Api.Applications.Orders.Queries;

[ExtendObjectType("Query")]
public class OrderQueryResolver
{
    public async Task<List<OrderGraphType>> GetOrders([Service] IRepository<Order, Guid> orderRepository)
    {
        var orders = await orderRepository.GetListAsync();
        return orders.Select(o => new OrderGraphType()
        {
            Id = o.Id,
            Name = o.Name,
        }).ToList();
    }

    public async Task<OrderGraphType> GetFirstOrder([Service(ServiceKind.Synchronized)] IRepository<Order, Guid> orderRepository)
    {
        var queryable = await orderRepository.GetQueryableAsync();
        var order = await queryable.FirstAsync();
        return new OrderGraphType()
        {
            Id = order.Id,
            Name = order.Name
        };
    }

    public async Task<List<OrderGraphType>> GetOrderByName(string name, [Service(ServiceKind.Synchronized)] IRepository<Order, Guid> orderRepository)
    {
        var queryable = await orderRepository.GetQueryableAsync();
        var orders = await queryable.Where(o => o.Name.Contains(name)).ToListAsync();
        return orders.Select(o => new OrderGraphType()
        {
            Id = o.Id,
            Name = o.Name,
        }).ToList();
    }
}